package com.WISSC.controller;


import com.WISSC.bean.User;
import com.WISSC.bean.layuiBean.ResultPage;
import com.WISSC.bean.layuiBean.VoPage;
import com.WISSC.commen.BaseController;
import com.WISSC.service.*;
import com.WISSC.util.Respons.ResponseUtil;
import com.WISSC.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员用户 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-05-05
 */
@Controller
@RequestMapping("/user")
@Api(value = "用户controller", tags = {"用户类型类"})
public class UserController extends BaseController {

    @Autowired
    UserService userService;
    @Autowired
    AuthorizationService authorizationService;
    @Autowired
    StudentService studentService;
    @Autowired
    OccupationService occupationService;
    @Autowired
    WebPageService webPageService;
    @Autowired
    ConfigureService configureService;

    @RequestMapping(value = "index.html",method = RequestMethod.GET)
    public String index(Model model){
        model.addAttribute("url",configureService.getById(1).getConfigure());
        return "index";
    }

    //跳登陆页面
    @RequestMapping(value = "login.html",method = RequestMethod.GET)
    public String login(Model model){
        model.addAttribute("url",configureService.getById(1).getConfigure());
        model.addAttribute("ding",configureService.getById(2).getConfigure());
        model.addAttribute("di",configureService.getById(3).getConfigure());
        return "login";
    }

    @RequestMapping(value = "adminlogin.html",method = RequestMethod.GET)
    public String adminlogin(Model model){
        model.addAttribute("url",configureService.getById(1).getConfigure());
        return "adminlogin";
    }

    //欢迎页
    @RequestMapping(value = "welcome.html",method = RequestMethod.GET)
    public String welcome(Model model){
        model.addAttribute("authorization",authorizationService.countPerson());
        model.addAttribute("student",studentService.countPerson());
        model.addAttribute("occupation",occupationService.countPerson());
        System.out.println("model:"+model);
        return "welcome";
    }
    @RequestMapping(value = "welcome1.html",method = RequestMethod.GET)
    public String welcome1(){
        return "welcome1";
    }
    //用户管理
    @RequestMapping(value = "user.html",method = RequestMethod.GET)
    public String user(){
            return "system/user/user";
    }
    //用户添加
    @RequestMapping(value = "user_add.html",method = RequestMethod.GET)
    public String insertUser(){
        return "system/user/user_add";
    }
    //用户编辑
    @RequestMapping(value = "user_edit.html",method = RequestMethod.GET)
    public String editUser(Model model,String ids){
        System.out.println("编辑ids:"+ids);
        model.addAttribute("model",ids);
        return "system/user/user_edit";
    }

    @RequestMapping(value = "aaa.html")
    public String aa(){
         return "lunbotu";
    }

    @RequestMapping(value = "bootstrap.html",method = RequestMethod.GET)
    public String bootstrap(){
        return "system/bootstrap";
    }

    @RequestMapping(value = "kongbai.html",method = RequestMethod.GET)
    public String kongbai(){
        return "system/kongbai";
    }

    /**
     * @param user
     * @return 登陆
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public boolean login(User user, HttpSession session) {
        System.out.println("ControllerUser:"+user);
        if (userService.login(user, session)) {
            return true;
        }
        return false;
    }


    /**
     * @param user
     * @return 添加用户
     */
    @RequestMapping(value = "insertUser",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加用户")
    public Map save(@RequestBody User user){
        user.setCreateTime(DateUtil.date());
        if(userService.save(user)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }


    /**
     * @param
     * @return 结合bootstrap 删除用户
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除用户")
    public Map delete(@RequestBody  User user){
        if(userService.removeById(user)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();

    }


    /**
     * @param user
     * @return 修改用户
     */
    @RequestMapping(value = "updateUser",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改用户")
    public Map update(@RequestBody User user){
        if(userService.updateById(user)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }


    /**
     * @param
     * @param
     * @return 查询分页
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询用户分页")
    public ResultPage<User> selectMyPage(VoPage voPage, String username){
        return userService.selectMyPage(voPage,username);
    }







        //搜索
    @RequestMapping(value = "sousuo",method = RequestMethod.GET)
    @ResponseBody
    public List<User> sousuo(String username){
        return userService.sousuo(username);
    }
    //批量删除
    @RequestMapping(value = "deleteBtId",method = RequestMethod.POST)
    @ResponseBody
    public Map deleteById(String  ids){
        System.out.println("ids:"+ids);
        String [] a = ids.split(",");
        for(int i=1;i<a.length;i++){
            System.out.println("a:"+a[i]);
            userService.removeById(a[i]);
        }
        return ResponseUtil.getSuccessMap();
    }
    //添加用户以及权限
    @RequestMapping("/saveUser")
    @ResponseBody
    public Map<String, Object> saveUser(String username,String password,Integer status, String menuId,HttpSession session) {
        return  userService.saveUser(username,password,status,menuId,getUser(session).getId());

    }

    @RequestMapping("/findMenu")
    @ResponseBody
    public User findMenu(HttpSession session){
        User user=new User();
        user=(User)session.getAttribute("user");
        return user;
    }


}
